import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Echocom {

    private String sever;
    private int port;
    private DatagramSocket ds;

    public Echocom(String s, int p) throws SocketException {
        sever = s;
        port = p;
        ds = new DatagramSocket();
    }

    public void start() throws IOException {


        Scanner scanner = new Scanner(System.in);
        while (true) {

            System.out.println("请输入请求: ");
            if (!scanner.hasNext()) {
                break;
            }
            String s = scanner.next();

            DatagramPacket p = new DatagramPacket(s.getBytes(),s.getBytes().length, InetAddress.getByName(sever),port);
            ds.send(p);

            DatagramPacket p1 = new DatagramPacket(new byte[4096], 4096);
            ds.receive(p1);
            System.out.println("返回响应: ");
            String s1 = new String(p1.getData(),0,p1.getLength());
            System.out.println(s1);

        }
    }

    public static void main(String[] args) throws IOException {

        Echocom echocom = new Echocom("127.0.0.1", 9099);

        echocom.start();
    }
}
